#include "characterclass.h"
#include "skill.h"
#include "classlevel.h"


#include <QTimer>


CharacterClass::CharacterClass(QObject *parent) :
    DTwentyItem(parent),
    d(new Data())
{
    qRegisterMetaType<ClassLevels*>();
    qRegisterMetaType<Skills*>();

    QTimer::singleShot(0, this, SLOT(onInitChildren()));

}

CharacterClass::CharacterClass(const CharacterClass& other) :
    DTwentyItem(other),
    d(other.d)
{
}

int CharacterClass::type() const
{
    return qMetaTypeId<CharacterClass*>();
}

CharacterClass::HitDie CharacterClass::hitDie() const
{
    return d->hitDie;
}

void CharacterClass::setHitDie(HitDie hitDie)
{
    d->hitDie = hitDie;
}

CharacterClass::SkillPoints CharacterClass::skillPoints() const
{
    return d->skillPoints;
}

void CharacterClass::setSkillPoints(SkillPoints skillPoints)
{
    d->skillPoints = skillPoints;
}

int CharacterClass::calcBaseBonus(int level, int category)
{
    switch (category)
    {
    case Excellent:
        return level;

    case Ordinary:
        return (level-1) - ((level-1)/4);

    case Poor:
        return (level-1)/2;

    default:
        break;
    }

    return -1;
}

int CharacterClass::calcSaveBonus(int level, int category)
{
    switch (category)
    {
    case Normal:
        return (level/3);

    case Good:
        return (level/2)+2;

    default:
        break;
    }

    return -1;
}

void CharacterClass::onInitChildren()
{
#if 0
    QString levelName = QString("%1 %2").arg(name()).arg(tr("Levels"));
    QString skillsName = QString("%1 %2").arg(name()).arg(tr("Class Skills"));
#endif

    QString levelName = tr("Levels");
    QString skillsName = tr("Class Skills");

    initChild<ClassLevels>(levelName);

    Skills* classSkills = initChild<Skills>(skillsName);
    Skills* campaignSkills = rootItem()->findChild<Skills*>(QString(), Qt::FindDirectChildrenOnly);

    if (!campaignSkills)
    {
        return;
    }

    classSkills->setValueSource(campaignSkills);
}
